﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rotempco.Website.Logic;
using Rotempco.Core.Web.CRUD.Objects;
using Rotempco.Core.Web.Logic;

namespace Rotempco.Website.MasterPages.UserControls
{
    public partial class DefaultPageList : System.Web.UI.UserControl
    {
        private bool _ShowEditButton = false;
        private bool _ShowDeleteButton = false;
        private string _EditUrl = "Edit.aspx";
        private string _DeleteUrl = "Delete.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void SetDetailsList(PageListContainer detailPageContainer)
        {
            if (!string.IsNullOrWhiteSpace(detailPageContainer.EditUrl))
                _EditUrl = detailPageContainer.EditUrl;

            if (!string.IsNullOrWhiteSpace(detailPageContainer.DeleteUrl))
                _DeleteUrl = detailPageContainer.DeleteUrl;

            if (UrlLogic.AspxFileExist(_EditUrl))
                _ShowEditButton = true;

            if (UrlLogic.AspxFileExist(_DeleteUrl))
                _ShowDeleteButton = true;

            repeater_Headers.DataSource = detailPageContainer.Headers;
            repeater_Headers.DataBind();

            repeater_Rows.DataSource = detailPageContainer.DetailRows;
            repeater_Rows.DataBind();
        }

        protected bool ShowEditButton()
        {
            return _ShowEditButton;
        }

        protected bool ShowDeleteButton()
        {
            return _ShowDeleteButton;
        }

        protected string GetEditUrl(object idString)
        {
            long id = long.Parse(idString.ToString());
            return UrlLogic.GotoUrl(url: UrlLogic.GetStaticBaseUrl() + "/" + _EditUrl, id: id);
        }

        protected string GetDeleteUrl(object idString)
        {
            long id = long.Parse(idString.ToString());
            return UrlLogic.GotoUrl(url: UrlLogic.GetStaticBaseUrl() + "/" + _DeleteUrl, id: id);
        }

    }
}